<?php
/**
 * Created by PhpStorm.
 * User: LAKECASEY
 * Date: 2018/12/13
 * Time: 18:48
 */
namespace App\model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class Message extends Model{
    //绑定表
    protected $table = 'message';
    public $timestamps = false;
    /*
     * 添加数据
     */
    public function Addmessage($request)
    {
        //进行入库处理
        DB::beginTransaction();
        try{
            //新增数据
            $add= $this->insertGetId([
                'name' => $request['name'],
                'contact' => $request['phone'],
                'Internet'=>$request['internet'],
                'email'=>$request['email'],
                'message'=>$request['message'],
                'create_time'=>time(),
                ]);
            DB::commit();
            $data = [
                'res'=>'success',
                'code'=>'200',
                'msg'=>'感谢您的留言我们会倾听你的贵言!'
                ];
            return response()->json($data,200);
        }catch(\Exception $e)
        {
            return response()->json($e->getMessage(),400);
        }
    }

    /**
     * 获取留言
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAllmessage()
    {
        $data = self::orderBy('message.create_time','desc')->paginate(4);
        return $data;
    }
}